//
//  Hero.m
//  CandyCrushClone
//
//  Created by Petar on 3/15/15.
//  Copyright (c) 2015 Nestorov Engineering. All rights reserved.
//

#import "Hero.h"
#import "RWTCookie.h"

@implementation Hero

- (instancetype)initWithType:(NSInteger)type
{
    self = [super init];
    if (self) {
        
        _cookieType1Counter = 0;
        _cookieType2Counter = 0;
        _cookieType3Counter = 0;
        _cookieType4Counter = 0;
        _cookieType5Counter = 0;
        _cookieType6Counter = 0;
        _heroInfoFromServer = [NSArray new];
        _type = type;
        
    }
    return self;
}

/*
 * Reset all counters to 0
 */
-(void)resetCounters
{
    
    _cookieType1Counter = 0;
    _cookieType2Counter = 0;
    _cookieType3Counter = 0;
    _cookieType4Counter = 0;
    _cookieType5Counter = 0;
    _cookieType6Counter = 0;
    
}


/*
 * Updates counter values for cookies from given chain
 */
-(void)calculateScoreForChain:(RWTChain *)chain
{
    
    int value = 1;
    
    for (RWTCookie *cookie in chain.cookies) {
        
        switch ( cookie.cookieType ) {
            case 1:
                _cookieType1Counter += value;
                break;
            case 2:
                _cookieType2Counter += value;
                break;
            case 3:
                _cookieType3Counter += value;
                break;
            case 4:
                _cookieType4Counter += value;
                break;
            case 5:
                _cookieType5Counter += value;
                break;
            case 6:
                _cookieType6Counter += value;
                break;
                
            default:
                break;
        }
        
    }
    
}


- (void)addComboCookies:(NSInteger)comboCookiesToBeAdded
{
    
    //update counters
    _cookieType1Counter += comboCookiesToBeAdded;
    _cookieType2Counter += comboCookiesToBeAdded;
    _cookieType3Counter += comboCookiesToBeAdded;
    _cookieType4Counter += comboCookiesToBeAdded;
    _cookieType5Counter += comboCookiesToBeAdded;
    _cookieType6Counter += comboCookiesToBeAdded;
    
}


/*
 * Update the counters for cookies with server values to prevent manipulation!
 */
-(void)setCounterValuesFromServer
{
    
    // at index 0 is heroType
    // at indexes from 1 to 6 are values for the cookies counters
    for (int i = 1; i < [_heroInfoFromServer count]; i++) {
        
        NSInteger counterValue = [_heroInfoFromServer[i] integerValue];
        
        switch ( i ) {
            case 1:
                _cookieType1Counter = counterValue;
                break;
            case 2:
                _cookieType2Counter = counterValue;
                break;
            case 3:
                _cookieType3Counter = counterValue;
                break;
            case 4:
                _cookieType4Counter = counterValue;
                break;
            case 5:
                _cookieType5Counter = counterValue;
                break;
            case 6:
                _cookieType6Counter = counterValue;
                break;
                
            default:
                break;
        }
        
    }
    
}


- (void)decreaseCookieCounters
{
    
    // at indexes from 1 to 6 are values for the cookies counters
    for (int i = 1; i <= [_usageConditions count]; i++) {
        
        NSString *usageKey = [NSString stringWithFormat:@"%d", i];
        NSInteger usageValue = [_usageConditions[usageKey] integerValue];
        
        // Updates Player and Hero counters by Hero Skills
        switch ( i ) {
                
            case 1:
                _cookieType1Counter -= usageValue;
                break;
                
            case 2:
                _cookieType2Counter -= usageValue;
                break;
                
            case 3:
                _cookieType3Counter -= usageValue;
                break;
                
            case 4:
                _cookieType4Counter -= usageValue;
                break;
                
            case 5:
                _cookieType5Counter -= usageValue;
                break;
                
            case 6:
                _cookieType6Counter -= usageValue;
                break;
                
            default:
                break;
        }
        
    }
    
}


@end
